package uiLayer.util;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder;

public abstract class MainPanelTemplate extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JPanel buttonPanel;
	private JSplitPane splitPane;
	private JPanel leftPanel;
	private JPanel leftCenterPanel;
	private JScrollPane scrollPane;

	public MainPanelTemplate() {
		setLayout(new BorderLayout(0, 0));
		
		this.setSplitPane(new JSplitPane());
		splitPane.setOneTouchExpandable(true);
		add(splitPane, BorderLayout.CENTER);
		splitPane.setContinuousLayout(true);
		splitPane.setResizeWeight(0.5);
		
		this.setLeftPanel(new JPanel());
		splitPane.setLeftComponent(leftPanel);
		leftPanel.setLayout(new BorderLayout(0, 0));
		
		this.setButtonPanel(new JPanel());
		leftPanel.add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.TRAILING);
		
		JButton btnNew = new JButton("Ny");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleNew();
			}
		});
		buttonPanel.add(btnNew);
		
		JButton btnEdit = new JButton("Rediger");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				handleEdit();
			}
		});
		buttonPanel.add(btnEdit);
		
		JButton btnDelete = new JButton("Slet");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleDelete();
			}
		});
		buttonPanel.add(btnDelete);
		
		{
			leftCenterPanel = new JPanel();
			leftPanel.add(leftCenterPanel, BorderLayout.CENTER);
			leftCenterPanel.setLayout(new BorderLayout(0, 0));
			{
				scrollPane = new JScrollPane();
				leftCenterPanel.add(scrollPane, BorderLayout.SOUTH);
			}
		}

		splitPane.setDividerLocation(0.25);

	}
	
	private void handlePrint() {
		doPrint();
	}
	

	protected abstract void doPrint();

	private void handleEdit() {
		doEdit();
	}
	
	protected abstract void doDelete();
	
	private void handleDelete() {
		doDelete();
	};

	protected abstract void doEdit();

	private void handleNew() {
		doNew();
	}

	protected abstract void doNew();

	
	protected void setSplitPane(JSplitPane jsp) {
		this.splitPane = jsp;
	}
	
	public JSplitPane getSplitPane() {
		return this.splitPane;
	}

	/**
	 * @return the buttonPanel
	 */
	protected JPanel getButtonPanel() {
		return buttonPanel;
	}

	/**
	 * @param buttonPanel the buttonPanel to set
	 */
	protected void setButtonPanel(JPanel buttonPanel) {
		this.buttonPanel = buttonPanel;
	}

	/**
	 * @return the centerPanel
	 */
	protected JPanel getCenterPanel() {
		return leftCenterPanel;
	}

	/**
	 * @param centerPanel the centerPanel to set
	 */
	protected void setCenterPanel(JPanel centerPanel) {
	}

	/**
	 * @return the scrollPane
	 */
	protected JScrollPane getScrollPane() {
		return scrollPane;
	}

	/**
	 * @param scrollPane the scrollPane to set
	 */
	protected void setScrollPane(JScrollPane scrollPane) {
	}

	/**
	 * @return the leftPanel
	 */
	protected JPanel getLeftPanel() {
		return leftPanel;
	}

	/**
	 * @param leftPanel the leftPanel to set
	 */
	protected void setLeftPanel(JPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

}
